package com.lxb.myblog.service.Impl;

import com.lxb.myblog.dao.TagDao;
import com.lxb.myblog.po.Tag;
import com.lxb.myblog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TagServiceImpl implements TagService {

    @Autowired
    public TagDao tagDao;

    @Override
    public List<Tag> getAllTag() {
        return tagDao.getAllTag();
    }

    @Override
    public List<Tag> listTag(String tagIds) {
        List<Integer> ids = convertToInt(tagIds);
        List<Tag> tags = new ArrayList<>();
        for (Integer tagId : ids) {
            Tag tag = tagDao.getTag(tagId);
            tags.add(tag);
        }
        return tags;
    }

    @Override
    public int updateTag(Tag tag) {
        return tagDao.updateTag(tag);
    }

    @Override
    public int saveTag(Tag tag) {
        return tagDao.saveTag(tag);
    }

    @Override
    public void deleteTag(Integer id) {
        tagDao.deleteTag(id);
    }

    @Override
    public Tag getTagById(Integer id) {
        return tagDao.getTag(id);
    }

    public List<Integer> convertToInt(String ids) {
        List<Integer> list = new ArrayList<>();
        if (!"".equals(ids) && ids != null) {
            String[] idArray = ids.split(",");
            for (String s : idArray) {
                list.add(new Integer(s));
            }
        }
        return list;
    }
}
